﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ToolBoxPro;
/// <summary>
/// Summary description for GetUserGroupRef
/// </summary>

    public class GetUserGroupRef : Page
    {
        private int userRef;
        private int agent_id;
        private int agent_group;
        private int contractor_id;

        public GetUserGroupRef(IDbConnection conn, String userRef)
        {
            this.userRef = Int32.Parse(userRef);
            string agent_script =
                                @"select top 1 agent_id, agent_group from agent where user_id = @user_id";
            string contractor_script =
                                @"select top 1 contractor_id, contractor_name from contractor where user_id = @user_id";

            if (conn == null)
                return;
            using (conn)
            {
                if (conn.State == ConnectionState.Open)
                {
                    IDbCommand cmd = conn.CreateCommand();
                    cmd.CommandText = agent_script;
                    IDbDataParameter param = cmd.CreateParameter();
                    param.ParameterName = "@user_id";
                    param.DbType = DbType.Int32;
                    param.Value = this.userRef;
                    cmd.Parameters.Add(param);
                    IDataReader dr = cmd.ExecuteReader();
                    if (dr.Read())
                    {
                        if (dr.GetInt32(0) > 0)
                        {
                            this.agent_id = dr.GetInt32(0);
                            this.agent_group = dr.GetInt32(1);
                            Session[FormContract.AGENT] = this.agent_id;
                            Session[FormContract.AGENT_GROUP_ID] = this.agent_group;

                        }
                        return;
                    }

                    cmd = conn.CreateCommand();
                    cmd.CommandText = contractor_script;
                    param = cmd.CreateParameter();
                    param.ParameterName = "@user_id";
                    param.DbType = DbType.Int32;
                    param.Value = Int32.Parse(userRef);
                    cmd.Parameters.Add(param);

                    dr = cmd.ExecuteReader();

                    if (dr.Read())
                    {

                        if (dr.GetInt32(0) > 0)
                        {
                            this.contractor_id = dr.GetInt32(0);
                            Session[FormContract.CONTRACTOR_ID] = this.contractor_id;
                            Session[FormContract.GREETING_NAME] = dr.GetString(1);
                        }
                        return;
                    }
                }
            }
        }

        public bool redirect()
        {
            if (this.agent_id > 0)
            {
                System.Web.HttpContext.Current.Response.Redirect("property.aspx", false);
                return true;
            }
            else if (this.contractor_id > 0)
            {
                System.Web.HttpContext.Current.Response.Redirect("task.aspx", false);
                return true;
            }
            else
                return false;
        }    
}
